//
//  AIDiaryReplayBean.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/3/11.
//
/**
 * ai日记 - minmax
 */
import Foundation
import SwiftUI
class AIDiaryReplayBeanMinmaxBean : Codable {
    
    var tokens_to_generate: Int64 = 1034
    var model = "abab6.5s-chat"
    var temperature: Double = 0.9
    var top_p: Double = 0.95
    var messages: [Messages]
    var reply_constraints: ReplyConstraintsc //回复格式
    var bot_setting:[BotSetting] //对每一个机器人的设定
    
    
    init(content : String, botRoleBean: RoleBean, listImage: [String]) {
        let userRoleBean = RoleBean.get(userManager.user, botRoleBean)
        self.reply_constraints = ReplyConstraintsc(aiRole: botRoleBean)
        self.bot_setting = [BotSetting(aiRole: botRoleBean)]
        self.messages = AIDiaryReplayBeanMinmaxBean.getMessagesList(content: content, botRoleBean: botRoleBean, userRoleBean: userRoleBean, listImage: listImage)
        
    }
    
    /**
     * 模型回复要求.
     */
    class ReplyConstraintsc : Codable{
        var sender_type = "BOT"
        var sender_name = ""  //指定回复的机器人名称。
        
        init(aiRole: RoleBean) {
            self.sender_name = aiRole.name ?? ""
        }
        
    }
    
    /**
     * 对每一个机器人的设定。
     * @param content 具体机器人的设定,长度影响接口性能。
     */
    class BotSetting : Codable {
        var bot_name: String
        var content: String
        init(aiRole: RoleBean) {
            bot_name = aiRole.name ?? ""
            content = RoleBean.getBotDesc(aiRole)
        }
    }
    
    class Messages: Codable {
        var sender_type = "USER"
        var text: String
        var sender_name: String
        
        init(_ content: String,_ botRoleBean: RoleBean) {
            var name = AiChatExt.getName2(botRoleBean)
            if name.isEmpty == true {
                name = "亲爱的"
            }
            self.text = content
            self.sender_name = name
        }
    }
    
    private static func getMessagesList(content : String, botRoleBean: RoleBean,
                                        userRoleBean: RoleBean, listImage: [String]) ->[Messages] {
        var listMsg : [Messages] = []

        //用户人设
        let userDesc: String = RoleBean.getUserDesc(botRoleBean)
        listMsg.append(Messages(userDesc,botRoleBean))
        //添加时间
        let timeStr1 = TimeUtils.getDate(date: Date(), format: "yyyy-MM-dd HH:mm")
        listMsg.append(Messages("现在的时间是：\(timeStr1),这条信息仅用于你理解，不用说出来。",botRoleBean))
        
        listMsg.append(Messages("这是我写的日记原文:`\(content)`。",botRoleBean))
        
        var hasImage = false
        if (listImage.isEmpty == false) {
            hasImage = true
            var stringBuffer = ""
            listImage.enumerated().forEach { index, path in
                stringBuffer += "图\(index + 1):\(path)"
            }
            listMsg.append(
                Messages("这是我发的图片信息:`\(listImage.map { $0 }.joined(separator: ";"))`。", userRoleBean)
            )
        }
        
        let imageMsg = hasImage ? "和我发的图片" : ""
        
        listMsg.append(Messages("接下来请你结合我写的日记\(imageMsg),\(configManager.getConfig().getAiDescribe().diaryDescribe ?? "")".formatAiChat(botRoleBean, Date().timeIntervalSince1970), userRoleBean))
        return listMsg
    }
    
    
}
